package com.xcdh.trading.task;

import cn.hutool.core.date.DateUtil;
import com.xcdh.trading.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.CompletableFuture;

@Component
public class SyncTrDataTask {
    @Autowired
    private HotMoneyTrcService hotMoneyTrcService ;

    @Autowired
    private CapitalFlowService capitalFlowService ;

    @Autowired
    private SectorCapitalFlowService sectorCapitalFlowService ;

    @Autowired
    private IndustryCapitalFlowService industryCapitalFlowService ;

    @Autowired
    private DailyIndicatorsService dailyIndicatorsService ;

    @Autowired
    private StockBiddingService stockBiddingService ;

    @Autowired
    private DayTransactionDataService dayTransactionDataService ;

    @Scheduled(cron = "0 0 4 * * ?")
    public void synAllTrData() {
        String tradeDate = DateUtil.format(new Date(), "yyyMMdd");
        CompletableFuture.runAsync(()->{
            sectorCapitalFlowService.syncSectorCapitalFlow(tradeDate);
            industryCapitalFlowService.syncIndustryCapitalFlow(tradeDate);
        }) ;
        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
            hotMoneyTrcService.syncHotMoneyTrc(tradeDate);
            capitalFlowService.syncCapitalFlow(tradeDate);
            dailyIndicatorsService.syncDailyIndicators(tradeDate);
            stockBiddingService.syncDayBidding(tradeDate);
            return 0;
        }).thenAccept((i) -> {
            dayTransactionDataService.syncTransactionData(tradeDate);
        });
        CompletableFuture.allOf(future).join() ;
    }

}
